﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor
{
    public interface IResourcePreview
    {
        void LoadResource(GraphicsDevice _device, ContentManager _episodeContent, String _resource);
        void Initialize(GraphicsDevice _device, ContentManager _localContent);
        void Update(float _elapsedSeconds);
        void Draw(GraphicsDevice _device, Jemgine.Engine.Renderer.Camera _camera);
        void MouseDown(Vector2 _mousePos);
        void MouseUp(Vector2 _mousePos);
        void MouseMove(Vector2 _mousePos);
        void Unload();
    }

    public class TexturePreview : IResourcePreview
    {
        Texture2D Texture;
        SpriteBatch Batch;


        public void Initialize(GraphicsDevice _device, ContentManager _localContent)
        {
            Batch = new SpriteBatch(_device);
        }

        public void LoadResource(GraphicsDevice _device, ContentManager _episodeContent, String _resource)
        {
            if (String.IsNullOrEmpty(_resource)) return;
            Texture = _episodeContent.Load<Texture2D>(_resource);
        }

        public void Update(float _elapsedSeconds)
        {
        }

        public void Draw(GraphicsDevice _device, Engine.Renderer.Camera _camera)
        {
            Batch.Begin();
            if (Texture != null) Batch.Draw(Texture, Vector2.Zero, Microsoft.Xna.Framework.Color.White);
            Batch.End();
        }

        public void MouseDown(Vector2 _mousePos)
        {
        }

        public void MouseUp(Vector2 _mousePos)
        {
        }

        public void MouseMove(Vector2 _mousePos)
        {
        }

        public void Unload() { }
    }

    public class EffectPreview : IResourcePreview
    {
        Jemgine.Particles.ParticleRenderer ParticleRenderer = new Particles.ParticleRenderer();
        Jemgine.Particles.ParticleEffect Effect = null;

        public void LoadResource(GraphicsDevice _device, ContentManager _episodeContent, String _resource)
        {
            if (Effect != null) Effect.ReleaseBuffers();
            if (String.IsNullOrEmpty(_resource)) return;
            Effect = _episodeContent.Load<Jemgine.Particles.ParticleEffect>(_resource);
            Effect.Initialize(_device, _episodeContent);
        }

        public void Initialize(GraphicsDevice _device, ContentManager _localContent)
        {
            ParticleRenderer.Initialize(_device, _localContent);
        }

        public void Update(float _elapsedSeconds)
        {
            if (Effect != null)
            {
                if (IsMouseDown) Effect.Emit(MousePos, 0);
                Effect.Update(_elapsedSeconds);
            }
        }

        public void Draw(GraphicsDevice _device, Jemgine.Engine.Renderer.Camera _camera)
        {
            if (Effect != null)
            {
                ParticleRenderer.View = _camera.View;
                ParticleRenderer.World = _camera.World;
                ParticleRenderer.Projection = _camera.Projection;
                ParticleRenderer.Draw(Effect);
            }
        }

        bool IsMouseDown = false;
        Vector2 MousePos = Vector2.Zero;

        public void MouseDown(Vector2 _mousePos)
        {
            MousePos = _mousePos;
            IsMouseDown = true;
        }

        public void MouseUp(Vector2 _mousePos)   
        {
            MousePos = _mousePos;
            IsMouseDown = false;
        }

        public void MouseMove(Vector2 _mousePos)
        {
            MousePos = _mousePos;
        }

        public void Unload()
        {
            if (Effect != null) Effect.ReleaseBuffers();
        }
    }
}
